﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Publishing;

namespace XYZ.Acknowledge
{
    public class acknowledge : PublishingLayoutPage 
    {
        //page control references here
        protected Button btnAccept;
        protected Button btnDecline;
        protected UserProfileManager pManager;

        //protected override void OnInit(EventArgs e)
        //{
        //    btnAccept.Click += new EventHandler(btnAccept_Click);
        //    btnDecline.Click += new EventHandler(btnDecline_Click);
        //    base.OnInit(e);
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb currentWeb = site.RootWeb)
                {
                    SPUser currentUser = currentWeb.CurrentUser;
                    //Create a new UserProfileManager
                    UserProfileManager pManager = new UserProfileManager();
                    //Get the User Profile for the current user and set vaulue to true
                    UserProfile uProfile = pManager.GetUserProfile(currentUser.LoginName);
                    uProfile["XYZENACKNOW"].Value = true;
                    uProfile.Commit();
                    //SPUtility.Redirect(currentWeb.Url.ToString(), SPRedirectFlags.Static, HttpContext.Current);  
                    //this.Page.Response.Redirect(currentWeb.Url.ToString());
                    this.Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                    this.Context.Response.End();
                }
            }
            //updates the user profile attribute and then redirect to the default page
        }
        protected void btnDecline_Click(object sender, EventArgs e)
        {
            //Response.Write("<h1><font color=red>" + "You cannot use the Employee Network until this policy is accepted" + "</font></h1><hr>");
            //close the browser event initiated from the client
            //StringBuilder sb = new StringBuilder();
            //sb.Append("<script type=\"text/javascript\">");
            //sb.Append("<!--");
            //sb.Append("if (confirm (\"You cannot use the Employee Network until this policy is accepted\") {");
            //sb.Append("window.location=\"http://www.yahoo.com/\"; }");
            //sb.Append("else {");
            //sb.Append("window.location=\"http://www.yahoo.com/\"; }");
            //sb.Append("//-->");
            //sb.Append("</script>");
            //Response.Write(sb.ToString());
        }
    }
}

